//
//  PopViewController.swift
//  funcode
//
//  Created by Wan Jiading on 2016/12/2.
//  Copyright © 2016年 WanJiading. All rights reserved.
//

import UIKit

class PopViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

		self.view.backgroundColor = UIColor.white
		
		
		let btn1 = createBtn("动态派发安全性", #selector(click1(_:)))
		btn1.frame.origin = CGPoint(x: 20, y: 100)
		self.view.addSubview(btn1)
		
		let btn2 = createBtn("横切关注点", #selector(click2(_:)))
		btn2.frame.origin = CGPoint(x: 20, y: 150)
		self.view.addSubview(btn2)
		
		let btn3 = createBtn("菱形缺陷", #selector(click3(_:)))
		btn3.frame.origin = CGPoint(x: 20, y: 200)
		self.view.addSubview(btn3)
		
    }
	
	func click1(_ btn: UIButton) {
		print("***" + btn.titleLabel!.text! + "***")
		let array: [Greetable] = [
			Person(name: "tom", age: 12),
			Cat(name: "tomMao"),
//			Dog()
		]
		for obj in array {
			obj.greet()
		}
	}
	
	func click2(_ btn: UIButton) {
		print("***" + btn.titleLabel!.text! + "***")
		Doctor().work()
		Teacher().work()
	}
	
	func click3(_ btn: UIButton) {
		let stu = Student(id: 1)
		DebugLog(stu.name)
	}
	
	
	private func createBtn(_ title: String, _ sel: Selector) -> UIButton {
		let btn = UIButton(type: .custom)
		btn.setTitle(title, for: .normal)
		btn.setTitleColor(UIColor.random, for: .normal)
		btn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
		btn.backgroundColor = UIColor.random
		btn.sizeToFit()
		btn.frame.size.width = btn.frame.size.width + 20
		btn.addTarget(self, action: sel, for: .touchUpInside)
		btn.layer.cornerRadius = 15
		return btn
	}
	
}
